import { __DEV__ } from '@/types/global';
import CryptoJS from 'crypto-js';

/**
 * @param path 文件名，默认 'default.svg'
 * @returns URL
 */
export const getAssetsImg = (path: string = 'default.svg') => {
    return new URL(`../assets/${path}`, import.meta.url).href
}

const _charStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/**
 * @param length 随机字符串长度，默认 20
 * @returns 随机字符串
 */
export const getRandomString = (length: number = 20) => {
    let result = '';
    const charactersLength = _charStr.length;
    for (let i = 0; i < length; i++) {
        result += _charStr.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

/**
 * @param data 需要加密的数据
 * @returns {string} 加密后的数据
 */
export const encrypt = (data: any) => {
    const key = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_ENCRYPT_SECRET_KEY)
    const iv = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_ENCRYPT_SECRET_IV)

    const srcs = CryptoJS.enc.Utf8.parse(data);
    const encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    })
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
 * @param data 需要解密的数据
 * @returns {string} 解密后的数据
 */
export const decrypt = (data: any) => {
    const key = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_ENCRYPT_SECRET_KEY)
    const iv = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_ENCRYPT_SECRET_IV)
    const base64 = CryptoJS.enc.Base64.parse(data);
    const srcs = CryptoJS.enc.Base64.stringify(base64);
    const decrypt = CryptoJS.AES.decrypt(srcs, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr;
}


/**
 * @param element 需要获取高度的元素
 * @returns 返回元素的高度
 */
export const getHeight = (element: Element): number => {
    if (!element) {
        if (__DEV__) console.error('getWidth 传入的元素不存在', 'element=', element);
        return 50
    }
    const styles = window.getComputedStyle(element);
    const marginTop = parseFloat(styles.marginTop);
    const marginBottom = parseFloat(styles.marginBottom);
    const borderTop = parseFloat(styles.borderTopWidth);
    const borderBottom = parseFloat(styles.borderBottomWidth);
    const paddingTop = parseFloat(styles.paddingTop);
    const paddingBottom = parseFloat(styles.paddingBottom);
    const height = element.clientHeight;
    return height + marginTop + marginBottom + borderTop + borderBottom + paddingTop + paddingBottom;
}
/**
 * @param element 需要获取宽度的元素
 * @returns 返回元素的宽度
 */
export const getWidth = (element: Element): number => {
    if (!element) {
        if (__DEV__) console.error('getWidth 传入的元素不存在', 'element=', element);
        return 50
    }
    const styles = window.getComputedStyle(element);
    const marginLeft = parseFloat(styles.marginLeft);
    const marginRight = parseFloat(styles.marginRight);
    const borderLeft = parseFloat(styles.borderLeftWidth);
    const borderRight = parseFloat(styles.borderRightWidth);
    const paddingLeft = parseFloat(styles.paddingLeft);
    const paddingRight = parseFloat(styles.paddingRight);
    const width = element.clientWidth;
    return width + marginLeft + marginRight + borderLeft + borderRight + paddingLeft + paddingRight;
}